package main

import (
	"fmt"
	"runtime"
	"sync"
)

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	c:= make(chan bool, 10)
	//go func(){
	//	fmt.Println("GO GO GO !!!")
	//	c <- true
	//	close(c)
	//}()
	//for v:= range c{
	//	fmt.Println(v)
	//}

	for i:=0; i<10; i++{
		go GO(c, i)
	}
	for i:=0; i<10; i++{
		<-c
	}
	fmt.Println("-------------------")
	wg := sync.WaitGroup{}
	wg.Add(10)
	for i:=0; i<10; i++{
		go GO1(&wg, i)
	}
	wg.Wait()

	c1, c2 := make(chan int), make(chan string)
	o := make(chan bool)
	go func(){
		for{
			select {
				case v, ok:= <-c1:
					if !ok{
						o<-true
						break
					}
					fmt.Println("c1",v)
				case v,ok:=<-c2:
					if !ok{
						o<-true
						break
					}
					fmt.Println("c2",v)
			}
		}


	}()
	c1 <- 1
	c2 <- "h1"
	c1 <- 3
	c2 <- "hello"

	close(c1)
	close(c2)

	<-o
}

func GO(c chan bool, index int){
	a:=1
	for i:=0; i<100000;i++{
		a += i
	}
	fmt.Println(index,a)
	c <- true
}

func GO1(wg *sync.WaitGroup, index int){
	a:=1
	for i:=0; i<100000;i++{
		a += i
	}
	fmt.Println(index,a)
	wg.Done()
}
